package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.CartList;
import com.example.lanouhn.firstapp.activity.model.CartListEdit;
import com.example.lanouhn.firstapp.activity.model.Syomb;
import com.example.lanouhn.firstapp.activity.utils.CartListEditJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.example.lanouhn.firstapp.activity.utils.SyombJson;
import com.example.lanouhn.firstapp.activity.utils.T;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 核对购物信息商品列表界面数据适配器
 * <p/>
 *
 */
public class CheckInformationAdapter extends RecyclerView.Adapter {

    //上下文对象
    private Context context;
    //数据集合
    private List<CartList> list;

    private String key;
    private Syomb slist;
    private CartListEdit cList;
    private String price;
    //设置recycle的监听事件接口

    /**
     * 定义接口里面实现点击事件方法
     */
    public interface CartListRecyclerViewListener {
        void onclick(View view, int postion);
    }

    //定义CartListRecyclerViewListener变量
    private CartListRecyclerViewListener cartListRecyclerViewListener;

    //注册事件
    public void setCartListRecyclerViewListener(CartListRecyclerViewListener
                                                        cartListRecyclerViewListener) {
        this.cartListRecyclerViewListener = cartListRecyclerViewListener;
    }

    public CheckInformationAdapter(Context context, List<CartList> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局
        RecyclerView.ViewHolder holder = null;

        holder = new VHolder(
                LayoutInflater.from(context).

                        inflate(R.layout.item_check_information, null));
        //获取sharedPreference中存储的key
        key = (String) SPUtils.get(context, "log_key", "");
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


        final VHolder vHolder = (VHolder) holder;
        //数据位置
        final CartList cartList = list.get(position);
        //设置数据 运用picasso获取图片
        Picasso.with(context).load(cartList.getGoods_image_url()).into(vHolder.goods_image_url);
        vHolder.goods_name.setText(cartList.getGoods_name());
        vHolder.goods_sum.setText(cartList.getGoods_sum());
        vHolder.goods_num.setText(cartList.getGoods_num());
        vHolder.goods_price.setText(cartList.getGoods_price());


        //item的点击事件
        vHolder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                cartListRecyclerViewListener.onclick(vHolder.itemView, position);
            }
        });

        //增加

        vHolder.btn_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = Integer.parseInt(String.valueOf(vHolder.goods_num.getText()));
                if (num > 1) {
                    num--;
                    vHolder.goods_num.setText(num + "");

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            //点击加入购物车的post请求url
                            String url_edit = "http://www.niaolema.com/mobile/index.php?act=member_cart&op=cart_edit_quantity";
                            String params_edit = "key=" + key + "&cart_id=" + cartList.getCart_id() + "&quantity=" + vHolder.goods_num.getText();
                            String result = HttpUtils.doPost(url_edit, params_edit);
                            cList = CartListEditJson.getCartListEdit(result);

                            if (cList.toString().length()>0) {
                                handler.sendEmptyMessage(1);
                            } else {
                                handler.sendEmptyMessage(0);
                            }
                        }
                    }).start();
                } else {
                    T.showShort(context, "购买数量不能少于1件");
                    vHolder.goods_num.setText(1 + "");
                }
            }

            private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    switch (msg.what) {
                        case 0:
                            T.showShort(context, "网络错误，请重试");
                            break;
                        case 1:
                            vHolder.goods_num.setText(cList.getQuantity());
                            vHolder.goods_sum.setText(cList.getTotal_price());

                            Intent intent = new Intent();
                            intent.setAction("aaaaa");
                            context.sendBroadcast(intent);
                            break;
                    }
                }
            };
        });
        //增加
        vHolder.btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = Integer.parseInt(String.valueOf(vHolder.goods_num.getText()));
                num++;
                vHolder.goods_num.setText(num + "");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //点击加入购物车的post请求url
                        String url_edit = "http://www.niaolema.com/mobile/index.php?act=member_cart&op=cart_edit_quantity";
                        String params_edit = "key=" + key + "&cart_id=" + cartList.getCart_id() + "&quantity=" + vHolder.goods_num.getText();
                        String result = HttpUtils.doPost(url_edit, params_edit);
                        cList = CartListEditJson.getCartListEdit(result);

                        if (cList.toString().length()>0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                }).start();

            }
            private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    switch (msg.what) {
                        case 0:
                            T.showShort(context, "网络错误，请重试");
                            break;
                        case 1:
                            vHolder.goods_num.setText(cList.getQuantity());
                            vHolder.goods_sum.setText(cList.getTotal_price());

                            Intent intent = new Intent();
                            intent.setAction("aaaaa");
                            context.sendBroadcast(intent);
                            break;
                    }
                }
            };
        });
//删除购物车中的商品
        vHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        String url = "http://www.niaolema.com/mobile/index.php?act=member_cart&op=cart_del";
                        String params = "key=" + key + "&cart_id=" + cartList.getCart_id();
                        String result = HttpUtils.doPost(url, params);
                        slist = SyombJson.getSyomb(result);

                        if (slist.getCode() == 200 && slist.getDatas() == 1) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                }).start();
            }

            private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    switch (msg.what) {
                        case 0:
                            T.showShort(context, "网络错误，请重试");
                            break;
                        case 1:
                            Intent intent = new Intent();
                            intent.setAction("aaaaa");
                            context.sendBroadcast(intent);
                            break;
                    }
                }
            };
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    //定义VHolder类继承RecyclerView.ViewHolder
    class VHolder extends RecyclerView.ViewHolder {


        private TextView goods_name;
        private TextView goods_price;
        private TextView goods_num;
        private ImageView goods_image_url;
        private TextView goods_sum;

        //点击事件按钮
        private TextView btn_add;
        private TextView btn_reduce;
        private ImageView delete;

        //绑定控件id
        public VHolder(View itemView) {
            super(itemView);
            goods_image_url = (ImageView) itemView.findViewById(R.id.goods_image_url);
            goods_name = (TextView) itemView.findViewById(R.id.goods_name);
            goods_price = (TextView) itemView.findViewById(R.id.goods_price);
            goods_num = (TextView) itemView.findViewById(R.id.goods_num);
            goods_sum = (TextView) itemView.findViewById(R.id.goods_sum);

            btn_add = (TextView) itemView.findViewById(R.id.btn_add);
            btn_reduce = (TextView) itemView.findViewById(R.id.btn_reduce);
            delete = (ImageView) itemView.findViewById(R.id.delete);

        }
    }
}
